package com.zhentao.utils;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @Author: 天山一剑卓不凡
 */
public class MinioUtil {
    public static void main(String[] args)  throws Exception{
        //minio上传文件
        upload();


    }

    private static void upload() throws IOException, ErrorResponseException, InsufficientDataException, InternalException, InvalidKeyException, InvalidResponseException, NoSuchAlgorithmException, ServerException, XmlParserException {
        //获取minio客户端连接
        MinioClient minioClient = MinioClient.builder()
                .endpoint("http://localhost:9000")
                .credentials("minioadmin", "minioadmin")
                .build();
        //校验存储桶是否存在bucket
//        minioClient.

        //minio上传文件
        FileInputStream fileInputStream = new  FileInputStream("D:\\picture\\tupian1.jpeg");
        //文件的上传属性参数
        PutObjectArgs build = PutObjectArgs.builder()
                .bucket("chejian")   //存储桶
                .object("tupian1.jpeg")  //文件名
                .stream(fileInputStream,fileInputStream.available(),-1)  //文件流，文件大小，分片大小
                .build();

        minioClient.putObject(build);

        System.out.println("上传成功");
        //释放资源
        fileInputStream.close();
    }
}
